package com.asr.controller;

import com.asr.model.ApiResponse;
import com.asr.service.FileService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/files")
@RequiredArgsConstructor
public class FileController {
    
    private final FileService fileService;
    
    /**
     * 上传单个音频文件
     */
    @PostMapping("/upload")
    public ApiResponse<String> uploadFile(@RequestParam("file") MultipartFile file) {
        try {
            String filePath = fileService.uploadAudioFile(file);
            return ApiResponse.success("文件上传成功", filePath);
        } catch (Exception e) {
            log.error("文件上传失败", e);
            return ApiResponse.error("文件上传失败: " + e.getMessage());
        }
    }
    
    /**
     * 批量上传音频文件
     */
    @PostMapping("/upload/batch")
    public ApiResponse<List<String>> uploadFiles(@RequestParam("files") List<MultipartFile> files) {
        try {
            List<String> filePaths = fileService.uploadAudioFiles(files);
            return ApiResponse.success("文件上传成功", filePaths);
        } catch (Exception e) {
            log.error("批量文件上传失败", e);
            return ApiResponse.error("文件上传失败: " + e.getMessage());
        }
    }
    
    /**
     * 列出已上传的音频文件
     */
    @GetMapping("/list")
    public ApiResponse<List<String>> listFiles() {
        try {
            List<String> files = fileService.listUploadedAudioFiles();
            return ApiResponse.success(files);
        } catch (Exception e) {
            log.error("获取文件列表失败", e);
            return ApiResponse.error("获取文件列表失败: " + e.getMessage());
        }
    }
    
    /**
     * 列出指定目录的音频文件
     */
    @GetMapping("/list/{directory}")
    public ApiResponse<List<String>> listFilesInDirectory(@PathVariable String directory) {
        try {
            List<String> files = fileService.listAudioFiles(directory);
            return ApiResponse.success(files);
        } catch (Exception e) {
            log.error("获取文件列表失败", e);
            return ApiResponse.error("获取文件列表失败: " + e.getMessage());
        }
    }
    
    /**
     * 删除文件
     */
    @DeleteMapping("/{filePath}")
    public ApiResponse<Void> deleteFile(@PathVariable String filePath) {
        boolean success = fileService.deleteFile(filePath);
        if (success) {
            return ApiResponse.success("文件已删除", null);
        } else {
            return ApiResponse.error("删除文件失败");
        }
    }
    
    /**
     * 获取文件信息
     */
    @GetMapping("/info")
    public ApiResponse<FileService.FileInfo> getFileInfo(@RequestParam String filePath) {
        try {
            FileService.FileInfo info = fileService.getFileInfo(filePath);
            if (info == null) {
                return ApiResponse.error(404, "文件不存在");
            }
            return ApiResponse.success(info);
        } catch (Exception e) {
            log.error("获取文件信息失败", e);
            return ApiResponse.error("获取文件信息失败: " + e.getMessage());
        }
    }
}
